package logic

import (
	"context"
	"fmt"
	"proto/models"

	"proto/protorpc/internal/svc"
	"proto/protorpc/protorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
	return &AddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddLogic) Add(in *protorpc.AddRequest) (*protorpc.AddResponse, error) {
	// todo: add your logic here and delete this line
	proto := models.Proto{
		Name:  in.Name,
		Price: float64(in.Price),
		Stock: in.Stock,
		Img:   in.Img,
	}
	err := models.DB.Create(&proto).Error
	if err != nil {
		return nil, fmt.Errorf("添加失败")
	}
	return &protorpc.AddResponse{
		Id: int64(proto.ID),
	}, nil
}
